<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 订单信息 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-lx-add" @click="handleEdit('-1', {})">添加</el-button>
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>

                <el-input v-model="query.name" placeholder="客户名称" class="handle-input mr10" clearable></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                row-key="id"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                @expand-change="expandChange"
                :expand-row-keys='expandRowKeys'
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="expand">
                    <template slot-scope="props">
                        <h3>订单编号：{{props.row.id}}</h3>
                        <el-table :data="orderItems" border class="itemTable" :ref="'itemTable'+props.row.id">
                            <el-table-column prop="itemId" label="编号"></el-table-column>
                            <el-table-column prop="productId" label="商品编号"></el-table-column>
                            <el-table-column prop="name" label="商品名称"></el-table-column>
                            <el-table-column prop="price" label="价格"></el-table-column>
                            <el-table-column prop="quantity" label="购买数量"></el-table-column>
                            <el-table-column prop="quantityShipped" label="发货数量"></el-table-column>
                        </el-table>
                    </template>
                </el-table-column>
                <el-table-column prop="id" label="订单编号" align="center"></el-table-column>
                <el-table-column prop="customer" label="客户名称"></el-table-column>
                <el-table-column prop="saleName" label="销售代表"></el-table-column>
                <el-table-column prop="dateOrdered" label="订货日期"></el-table-column>
                <el-table-column prop="dateShipped" label="发货日期"></el-table-column>
                <el-table-column prop="total" label="订单金额"></el-table-column>
                <el-table-column prop="paymentType" label="支付方式"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageNum"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="订单编号" v-if="this.form.id">
                    <el-input v-model="form.id" readonly></el-input>
                </el-form-item>
                <el-form-item label="客户名称">
                    <el-input v-model="form.customer"></el-input>
                </el-form-item>
                <el-form-item label="销售代表">
                    <el-input v-model="form.saleName"></el-input>
                </el-form-item>  
              <el-form-item label="订货日期">
                    <el-date-picker v-model="form.dateOrdered" type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss"> </el-date-picker>
                </el-form-item>
                <el-form-item label="发货日期">
                    <el-date-picker v-model="form.dateShipped" type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss"> </el-date-picker>
                </el-form-item>
                <el-form-item label="订金">
                    <el-input v-model="form.total" />
                </el-form-item>
                <el-form-item label="支付方式">
                    <el-input v-model="form.paymentType" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { getDeptByCondition, saveOrUpdateDept } from '../api/dept';
import {listOrderByPage,listOrderItem,findOrderByCondition,saveOrUpdateOrder} from "../api/order"
import { salesRepByRegion } from "../api/emp"
import { titleByPage } from "../api/title"
import { findCusByPage } from "../api/customer"
import RegionList from '../components/common/RegionList.vue';

export default {
    name: 'Customer',
    name: 'Order',
    components: {
        RegionList
    },
    data() {
        //定义和当前vue组件绑定的数据模型。
        return {
            query: {
                name: '',
                pageNum: 1,
                pageSize: 5
            },
            options:[],
            tableData: [],
            orderItems:[],
            expandRowKeys:[],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {
                regionId:'',
                salesRepId:''
            },
            idx: -1,
            id: -1
        };
    },
    created() {
        //生命周期函数
        this.getData();
        getDeptByCondition({ pageNum: 1, pageSize: 0 }).then((resp) => {
            this.depts = resp.data.list;
        });
        titleByPage({pageNum:1,pageSize:0}).then(resp=>{
            this.titles = resp.data.list
        })
    },
    methods: {
        expandChange(row,expandRow){
            if(this.expandRowKeys[0] == row.id){
                return;
            }
            this.expandRowKeys=[];
            this.expandRowKeys.push(row.id);
            listOrderItem(row.id).then(resp=>{
                this.orderItems = resp.data;
            });
        },
        changeRegion(val) {
            console.log("val",val);
            this.form.regionId = val;
            //根据区域查找所有销售代表的信息。
            salesRepByRegion(this.form.regionId).then(resp=>{
                this.options = resp.data;
                //this.form.salesRepId = ''
            })
        },
        //定义和当前vue组件绑定的方法。
        getData() {
            listOrderByPage(this.query)
                .then((res) => {
                    console.log(res);
                    this.tableData = res.data.list;
                    this.pageTotal = res.data.total || 0;
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        getCondition() {
            findOrderByCondition(this.query)
                .then((res) => {
                    console.log(res);
                    this.tableData = res.data.list;
                    this.pageTotal = res.data.total || 0;
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageNum', 1);
            this.getCondition();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    // deleteRegionById(row.id).then((resp) => {
                    //     this.$message.success(resp.data);
                    //     // this.tableData.splice(index,1);
                    //     // this.pageTotal--;
                    //     this.getData();
                    // });
                    // this.$message.success('删除成功');
                    // this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log('-------选项变化------', this.multipleSelection);
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            if (length <= 0) {
                this.$message.error('请至少选择一项进行删除！');
                return; //结束
            }
            //发送ajax请求，进行批量删除操作。
            let ids = [];
            let str = [];
            for (let i = 0; i < length; i++) {
                ids.push(this.multipleSelection[i].id);
                str.push(this.multipleSelection[i].name);
            }
            console.log('ids === ', ids);
            this.$confirm('是否要删除区域【' + str + '】', '提示', {
                type: 'warning'
            }).then(() => {
                // deleteRegionByBatch(ids).then((resp) => {
                //     this.$message.success(resp.data);
                //     this.getData(); //刷新页面
                // });
            });
        },
        // 编辑操作
        handleEdit(index, row) {
            console.log("员工信息row",row)
            this.idx = index;
            if(row.regionId){
             this.changeRegion(row.regionId);
            }
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            console.log('form', this.form);
            saveOrUpdateOrder(this.form)
                .then((resp) => {
                    this.$message.success(resp.data);
                    //关闭对话框，
                    this.editVisible = false;
                    this.getData();
                })
                .catch(() => {});
            //发送网络请求，请求后台进行保存或更新的服务
            //this.editVisible = false;
            //this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            //this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageNum', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 100px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.itemTable{
    width: 85%;
    font-size: 12px;
    margin: 0px auto;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 5px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>